<template>
    <div class="commoninfo">
        <Modal
        width="50%"
        v-model="mailModel"
        :footer-hide="true"
        :open-model="mailModel=true"
        :title="mailTitle"
        @on-cancel="cancelMail"
        >
            <div>
                <Form ref="wayBillForm" :model="wayBillForm" :rules="wayBillFormValiate" :label-width="140">
                    <Row>
                        <div>
                            <FormItem label="运单编号"  prop="pcompany" class="fl">
                                <Select v-model="wayBillForm.pcompany" placeholder="" style="width: 100px;">
                                    <Option :value="item.code" v-for="(item,index) in pcompanyList" :key="index">{{item.name}}</Option>
                                </Select>
                            </FormItem>
                            <FormItem label="" prop="postNo" class="postNo fl" :label-width="0">
                                <Input v-model="wayBillForm.postNo"  placeholder=""  class="w200" style="margin-left:-4px;"/>
                            </FormItem>
                        </div>
                    </Row>
                    <Row>
                        <FormItem label="邮寄人" prop="pUserId">
                            <Input v-model="wayBillForm.pUserId"  placeholder="" style="width: 300px;"
                            :disabled="true"
                            />
                        </FormItem>
                    </Row>
                    <Row>
                        <FormItem label="邮寄时间" prop="pDate">
                            <DatePicker type="date" placeholder="" v-model="wayBillForm.pDate" :options="mailDate"></DatePicker>
                        </FormItem>
                    </Row>
                    <Row>
                        <FormItem label="备注" prop="memo">
                            <Input v-model="wayBillForm.memo"  placeholder="" type="textarea"  style="width: 300px;"/>
                        </FormItem>
                    </Row>
                    <Row style="padding-left: 140px;">
                        <Button type="primary" @click="saveMail">保存</Button><Button @click="cancelMail" style="margin-left: 20px;">取消</Button>
                    </Row>
                </Form>
            </div>
        </Modal>
    </div>
</template>
<script>
       import {
        savePostInfo,
        editPostInfo,
        getCodes,
        newPostInfo,
    } from "_p/afs-apply/api/mail-manage/orderMailList.js";
    import getcodeList from "_p/afs-apply/api/mail-manage/json/getcodes.json"
    import {formatDate} from "@/components/iview/components/date-picker/util";
    export default {
        name:"",
        data(){
            return {
                id:"",
                mailTitle:"",
                postImgMd5:"",
                mailModel:false,
                wayBillForm:{
                    pcompany:"",
                    postNo:"",
                    pUserId:"测试数据",
                    pDate:"",
                    memo:"",
                },
                mailDate:{
                    disabledDate (date) {
                        return date && date.valueOf() >Date.now() - 8.64e6;
                    }
                },
                wayBillFormValiate:{
                    pcompany:[
                        {required: true, message: "快递公司不能为空", trigger: "change"}
                    ],
                    postNo:[
                        {required: true, message: "运单编号不能为空", trigger: "blur"}
                    ],
                    pUserId:[
                        {required: true, message: "邮寄人不能为空", trigger: "blur"}
                    ],
                    pDate:[
                        {required: true, message: "邮寄时间不能为空", trigger: "change",type:"date"}
                    ],
                }
            }
        },
        props:{
            operType:{
                type:String,
            },
            editId:{
                type:String
            },
            pcompanyList:{
                type:Array,
            }
        },
        mounted(){
          if(this.operType=="add"){
                this.mailTitle="创建运单"
          }else if(this.operType=='edit'){
                this.id=this.editId;
                this.editPostMsg();
                this.mailTitle="编辑运单"
          }
        },
        methods:{
            // 编辑回显运单信息
            editPostMsg(){
                let params={
                    id:this.id,
                };
                editPostInfo(params).then(res=>{
                    if(res.code=="0000"){
                        let obj={
                            pcompany:res.data.pCompany,
                            memo:res.data.memo,
                            pDate:res.data.pDate,
                            postNo:res.data.postNo,
                            pUserId:res.data.pUserId
                        }
                        this.wayBillForm=obj;
                    }
                })

            },
            cancelMail() {
                this.$emit("close-model", false)
            },
            // 保存邮寄
            saveMail(){
                this.$refs.wayBillForm.validate((valid)=>{
                    if(valid){
                        let pDate=formatDate(this.wayBillForm.pDate,"yyyy-MM-dd");
                        let params={
                            postNo:this.wayBillForm.postNo,
                            postCompany:this.wayBillForm.pcompany,
                            postDate:pDate,
                            postImgMd5:"64d8b95aa0d346892057380fce811a34",
                            memo:this.wayBillForm.memo,
                            id:this.id,//从创建订单接口中获取
                        }
                        savePostInfo(params).then((res)=>{
                            if(res.code=="0000"){
                                if(this.operType=='add'){
                                    this.$emit("showRegisterMail",res.data.id)
                                }else if(this.operType=='edit'){
                                    this.$emit("showRegisterMail")
                                }
                                this.$Message.success("保存运费邮寄成功");
                            }
                        })
                    }
                })
            },
        }
    }
</script>
<style>

</style>
